﻿cms.SitePreview = function () {

    var _borderDivs = [];

    function _init() {
        cms$(window).load(_onLoad);
        cms$(window).resize(function () {
            _removeBorderDivsFromAllEditableAreas();
            _addBorderDivsToAllEditableAreas();
        });
    }

    function _onLoad() {
        _updatePreviewIframeHeight();
        _addBorderDivsToAllEditableAreas();
        _createOverlay();
        _fixEmbeddedIframesTransparency();
    }

    function _updatePreviewIframeHeight() {
        var contentHeight = cms$(document).height();
        contentHeight = contentHeight < 30 ? 30 : contentHeight;
        contentHeight += 40;
        var iframes = window.parent.document.getElementsByTagName("iframe");
        for (var i = 0; i < iframes.length; i++) {
            if (iframes[i].className == "sitePreview") {
                iframes[i].style.height = contentHeight + "px";
            }
            else if (iframes[i].className == "contentPreview" && window.location.href == iframes[i].src) {
                iframes[i].style.height = contentHeight + "px";
                window.parent.iframeLoaded(iframes[i]);
            }
        }
    }

    function _addBorderDivsToAllEditableAreas() {
        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            switch (divs[i].className) {
                case "cmsPageContent":
                case "cmsMasterTemplateContent":
                case "cmsDetailTemplateContent":
                case "cmsApplicationPageContent":
                    var borderDiv = document.createElement("div");
                    borderDiv.className = divs[i].className + "Border";
                    divs[i].parentNode.appendChild(borderDiv);
                    borderDiv.style.top = divs[i].offsetTop + "px";
                    borderDiv.style.left = divs[i].offsetLeft + "px";
                    borderDiv.style.height = divs[i].offsetHeight - 2 + "px";
                    borderDiv.style.width = divs[i].offsetWidth - 2 + "px";
                    _borderDivs[_borderDivs.length] = borderDiv;
                    break;
            }
        }
    }

    function _fixEmbeddedIframesTransparency() {
        var iframes = document.getElementsByTagName("iframe");
        for (var i = 0; i < iframes.length; i++) {
            var url = iframes[i].getAttribute("src");
            url += url.indexOf("?") > 0 ? "&" : "?";
            url += "wmode=transparent";
            iframes[i].setAttribute("src", url);
        }
    }

    function _removeBorderDivsFromAllEditableAreas() {
        for (var i = 0; i < _borderDivs.length; i++) {
            _borderDivs[i].parentNode.removeChild(_borderDivs[i]);
        }
        _borderDivs = [];
    }

    function _getAreaWidth(a) {
        var areaWidth = a.parentNode.parentNode.offsetWidth;
        var location = a.getAttribute("data-location") + "&editorbodywidth=" + areaWidth;
        a.setAttribute("data-location", location);
    }

    function _createOverlay() {
        var div = document.createElement("div");
        div.style.display = 'block';
        div.style.zIndex = 1000;
        div.style.backgroundColor = "#000000";
        div.style.width = 100 + "%";
        div.style.height = 100 + "%";
        div.style.top = 0 + "px";
        div.style.left = 0 + "px";
        div.style.position = "fixed";
        div.style.overflow = "hidden";
        var opacity = 0;
        var opaque = (opacity / 100);
        div.style.opacity = opaque;
        div.style.MozOpacity = opaque;
        div.style.filter = 'alpha(opacity=' + opacity + ')';
        var form = document.getElementsByTagName("form")[0];
        form.parentNode.insertBefore(div, null);
    }

    return {
        Init: _init,
        GetAreaWidth: _getAreaWidth
    }

} ();




